package view;

import com.vaadin.Application;
import com.vaadin.ui.*;
import com.vaadin.ui.LoginForm.LoginEvent;
import com.vaadin.ui.Window.Notification;

public class ProjetocreApplication extends Application {

    private Panel painelPrincipal = new Panel();
    private VerticalLayout layoutPainelPrincipal = new VerticalLayout();
    private VerticalLayout layoutPrincipal = new VerticalLayout();
    private Panel painelTitulo = new Panel();
    private Window mainWindow = new Window("Projetocre Application");

    @Override
    public void init() {


        mainWindow.setSizeFull();
        layoutPrincipal.setSizeFull();
        layoutPainelPrincipal.setSizeFull();

        Label titulo = new Label("<h1> Área de Administração do Site do CRE</h1>", Label.CONTENT_XHTML);
        painelTitulo.addComponent(titulo);

        LoginForm lf = new LoginForm();
        lf.setWidth("140px");
        lf.setHeight("120px");
        lf.setUsernameCaption("Login:");
        lf.setPasswordCaption("Senha:");

        Label tituloLogin = new Label(" Faça o seu Login para entrar na área de Administração:");
        Label espaco = new Label("<pre>                                                                                               </pre>"
                + "<pre>                                                                                                                     </pre>", Label.CONTENT_XHTML);

        layoutPainelPrincipal.addComponent(tituloLogin);
        layoutPainelPrincipal.addComponent(espaco);
        layoutPainelPrincipal.addComponent(lf);
        layoutPainelPrincipal.setComponentAlignment(tituloLogin, Alignment.TOP_CENTER);
        layoutPainelPrincipal.setComponentAlignment(lf, Alignment.TOP_CENTER);
        layoutPainelPrincipal.setSpacing(true);
        layoutPainelPrincipal.setMargin(true, false, false, false);
        painelPrincipal.addComponent(layoutPainelPrincipal);
        painelPrincipal.setWidth("400px");
        painelPrincipal.setHeight("400px");

        layoutPrincipal.addComponent(painelTitulo);
        layoutPrincipal.addComponent(painelPrincipal);
        layoutPrincipal.setComponentAlignment(painelPrincipal, Alignment.TOP_CENTER);

        mainWindow.setContent(layoutPrincipal);
        setMainWindow(mainWindow);

        lf.addListener(new LoginForm.LoginListener() {

            public void onLogin(LoginEvent event) {
                if (event.getLoginParameter("username").equals("admin") && event.getLoginParameter("password").equals("cre")) {
                    System.out.println("teste teste");
                    painelPrincipal.removeAllComponents();
                    layoutPainelPrincipal.removeAllComponents();
                    painelPrincipal.setWidth("545px");
                    painelPrincipal.setHeight("500px");
                    JanelaAdmin janelaAdmin = new JanelaAdmin(mainWindow);
                    layoutPainelPrincipal.addComponent(janelaAdmin);
                    layoutPainelPrincipal.setComponentAlignment(janelaAdmin, Alignment.MIDDLE_CENTER);
                    painelPrincipal.addComponent(layoutPainelPrincipal);
                } else {
                    mainWindow.getWindow().showNotification(
                            "Erro! Login ou senha\n"
                            + " inválidos! ", Notification.TYPE_ERROR_MESSAGE);
                }
            }
        });


    }
}
